﻿using Avalonia.Controls.ApplicationLifetimes;
using AvaloniaNotes.AvaloniaNotesDBContext;
using AvaloniaNotes.Models;
using AvaloniaNotes.OpCommand;
using AvaloniaNotes.Views;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Channels;

namespace AvaloniaNotes.ViewModels;

public partial class MainViewModel : ViewModelBase
{
    public DbContext ctx = new NoteDBContext();

    public MainViewModel()
    {
        _NoteList.Add(new Note() 
        { 
            NoteId = 1,
            Content = "Avalonia是一个强大的框架，使开发人员能够使用.NET创建跨平台应用程序。它使用自己的渲染引擎绘制UI控件，确保在Windows、macOS、Linux、Android、iOS和WebAssembly等不同平台上具有一致的外观和行为。这意味着开发人员可以共享他们的UI代码，并在不同的目标平台上保持统一的外观和感觉。", 
            Color = "#ffa500"
        });
        _NoteList.Add(new Note() { NoteId = 2, Content = "1", Color = "#FF8EE6" });
        _NoteList.Add(new Note() { NoteId = 3, Content = "1", Color = "#F0FAFA" });
        _NoteList.Add(new Note() { NoteId = 4, Content = "1", Color = "#000000" });
        _NoteList.Add(new Note() { NoteId = 5, Content = "1", Color = "#007119" });
    }
    private List<Note> _NoteList=[];

    public List<Note> NoteList
    {
        get { return _NoteList; }
        set { 
            _NoteList = value;
            OnPropertyChanged(nameof(NoteList));
        }
    }
    private Note _NoteDetail=new();

    public Note NoteDetail
    {
        get { return _NoteDetail; }
        set
        {
            _NoteDetail = value;
            OnPropertyChanged(nameof(NoteDetail));
        }
    }
    public string Greeting => "Welcome to Avalonia!";

    public void Test() => Console.WriteLine("111");

    public void DelNote(int id) 
    {
        NoteList = NoteList.Where(x => x.NoteId != id).ToList();
    }

    public void EditNote(int id) 
    {
        NoteDetail = _NoteList.First(x => x.NoteId == id);
        var NoteWindow = new NoteDetailWindow
        {
            DataContext = NoteDetail
        };
        NoteWindow.Show();
    }

    public Command TestCMD
    {
        get
        {
            return new Command(Test);
        }
    }
    public Command DelNoteCMD
    {
        get
        {
            return new Command(DelNote);
        }
    }

    public Command EditNoteCMD
    {
        get
        {
            return new Command(EditNote);
        }
    }
}
